#include "MarioGame.h"

////column index constant
//#define IMGPATH 1
//#define SIZE 2
//#define WIDTH 3
//#define HEIGHT 4
//#define SPRITEPERROW 5
//#define COUNT 6
//#define ANIRATE 7
//
////game sprite constant
//#define BACKGROUND 0
//#define MARIOSTANDLEFT1 1
//#define MARIOSTANDRIGHT1 2
//#define MARIOMOVELEFT1 3
//#define MARIOMOVERIGHT1 4
//#define MARIOJUMPLEFT1 5
//#define MARIOJUMPRIGHT1 6

CMarioGame::CMarioGame(HINSTANCE hInstance, LPCSTR GameName, LPSTR lpCmdLine, int nShowCmd, int FPS, int ScreenWidth, int ScreenHeight)
	:Game(hInstance, GameName, lpCmdLine, nShowCmd, FPS, ScreenWidth, ScreenHeight)
{
	CScreen::getInstance();
	CScreen::setSize(_screenWidth,_screenHeight);
}

CMarioGame::~CMarioGame(void)
{
	delete mario;
}

void ReadMap(char* mapName)
{
	
}

void CMarioGame::GameUpdate()
{
	//_coll.isCollided(mario,brick1);
	//_coll.isCollided(mario,brick2);

	mario->Move();

	std::set<MovingObject*> tList = tree.getRoot()->getCollidableObject(mario->getRect());
	for (std::set<MovingObject*>::iterator it= tList.begin(); it != tList.end(); it++)
	{
		if (_coll.isIntersect(mario->getRect(),(*it)->getRect()))
				_coll.isCollided(mario,(*it));
	}

	/*std::set<MovingObject*> tList2 = tree.getRoot()->getCollidableObject(gomba->getRect());
	for (std::set<MovingObject*>::iterator it= tList2.begin(); it != tList2.end(); it++)
	{
		if (_coll.isIntersect(gomba->getRect(),(*it)->getRect()))
				_coll.isCollided(gomba,(*it));
	}
*/

	std::set<MovingObject*> objList = tree.getRoot()->getMovableObjects(CScreen::getRect());
	////objList.insert(mario);
	for (std::set<MovingObject*>::iterator checkedObj = objList.begin(); checkedObj != objList.end(); checkedObj++)
	{
		(*checkedObj)->Move();

		std::set<MovingObject*> tList = tree.getRoot()->getCollidableObject((*checkedObj)->getRect());
		for (std::set<MovingObject*>::iterator it= tList.begin();it != tList.end();it++)
		{
			if (_coll.isIntersect((*checkedObj)->getRect(),(*it)->getRect()));
				_coll.isCollided((*checkedObj),(*it));
		}

	}
	//tree.update(tree.checkCollision(tree.getRoot(),tree.getRoot()->getRect()));
	tree.update(tree.fixTree(tree.getRoot(),CScreen::getRect()));
	//tree.fixTree(tree.getRoot(),CScreen::getRect());
	//tree.checkCollision(tree.getRoot(),CScreen::getRect());


	/*mario->checkSpeed();
	mario->fallDown();*/
	int i=0;
}

void CMarioGame::RenderFrame()
{
	//std::set<MovingObject*> list = tree.getRoot()->getAllObjectList();
	std::set<MovingObject*> list = tree.getRoot()->getCollidableObject(CScreen::getRect());
	//std::set<MovingObject*> list = tree.getRoot()->getCollidableObject(mario->getRect());
	//std::set<MovingObject*> list1 = tree.getRoot()->getCollidableObject(gomba->getRect());
	//list.insert(list1.begin(),list1.end());
	//std::set<MovingObject*> list = tree.getRoot()->getCollidableObject(gomba->getRect());
	for (std::set<MovingObject*>::iterator it = list.begin();it!= list.end();it++)
		(*it)->Render();
	mario->Render();
	
}

void CMarioGame::LoadResource()
{
	_rm = new CResourceManager(_directX,L"maps\\map1.rc");
	_rm->LoadConfig();
	_rm->LoadResource();

	_background = _directX->LoadSurface(_rm->getLink(CResourceManager::BACKGROUND),D3DCOLOR_XRGB(0,0,0));

	brick1 = new MovingObject(10,10,0,0,0,CResourceManager::BRICK,_rm);

	gomba = new Gomba(500,100,0.5f,0,1000/_FPS,CResourceManager::GOMBAMOVE,_rm);
	gomba2 = new Gomba(550,100,0.5f,0,1000/_FPS,CResourceManager::GOMBAMOVE,_rm);

	mario = new CMario(10,0,0,0,1000/_FPS,CResourceManager::MARIOSTANDLEFT1,_rm);

	ifstream inp("Maps\\abc");
	float w,h;
	inp>>w;
	inp>>h;
	tree = QuadTree(w*32,h*32);

	int i;
	int j;
	int count = 0;
	while (inp)
	{
		for (int j = 0;j<h;j++)
			for (int k=0;k<w;k++)
			{
				inp>>i;
				if (i == 30)
				{
					MovingObject *brick = new MovingObject(k*32,j*32,0,0,0,CResourceManager::BRICK,_rm);
					tree.add(brick);
					count++;
				}
		}
	}
	inp.close();
	tree.add(brick1);
	//tree.add(gomba);
	//tree.add(gomba2);
}

void CMarioGame::OnKeyDown(int KeyCode)
{
	switch (KeyCode)
	{
	case DIK_LEFT:
		{
			/*if (mario->isMoving())
			{
				if (mario->getSprite()->getName() != _rm->getSprite(MARIOMOVELEFT1)->getName())
					mario->setSprite(*(_rm->getSprite(MARIOMOVELEFT1)));
			}
			else mario->setSprite(*(_rm->getSprite(MARIOSTANDLEFT1)));*/
			mario->MoveLeft();
			break;
		}
	case DIK_RIGHT:
		{
			/*if (mario->isMoving())
			{
				if (mario->getSprite()->getName() != _rm->getSprite(MARIOMOVERIGHT1)->getName())
					mario->setSprite(*(_rm->getSprite(MARIOMOVERIGHT1)));
			}
			else mario->setSprite(*(_rm->getSprite(MARIOSTANDRIGHT1)));*/
			mario->MoveRight();
			break;
		}
	case DIK_SPACE:
		{
			mario->Jump();
			break;
		}

	case DIK_A:
		{
			brick1->Translate(Vector(-2,0));
			break;
		}
		case DIK_D:
		{
			brick1->Translate(Vector(2,0));
			break;
		}
		case DIK_W:
		{
			brick1->Translate(Vector(0,-2));
			break;
		}
		case DIK_S:
		{
			brick1->Translate(Vector(0,2));
			break;
		}
	default:
		{
			/*if (mario->_isMoveLeft)
				mario->setSprite(*(_rm->getSprite(MARIOSTANDLEFT1)));
			else
				mario->setSprite(*(_rm->getSprite(MARIOSTANDRIGHT1)));*/
			break;
		}
	}
}